/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alwv.reports;

import java.awt.Component;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class FileTreeCellRenderer extends DefaultTreeCellRenderer {

    @Override
    public Component getTreeCellRendererComponent(JTree tree,
            Object value, boolean sel, boolean expanded, boolean leaf,
            int row, boolean hasFocus) {
        JLabel renderer = (JLabel) super.getTreeCellRendererComponent(
                tree, value, sel, expanded, leaf, row, hasFocus);
        System.out.println(value.toString());
        if (isDirectory(new File("reports"), value.toString())) {
            System.out.println("---Directory " + value.toString());
            if (expanded) {
                renderer.setIcon(openIcon);
            } else {
                renderer.setIcon(closedIcon);
            }
        } else {
            renderer.setIcon(leafIcon);
        }
        return renderer;
    }

    private boolean isDirectory(File f, String s) {
        if(f.isDirectory()){
            if(f.getName().equals(s)){
                return true;
            }
            else{
                File[] list = f.listFiles();
                boolean found = false;
                for(int i = 0; i < list.length; i++){
                    if(isDirectory(list[i],s)){
                        found = true;
                        break;
                    }
                }
                return found;
            }
        }
        else{
            return false;
        }
    }
}